package com.agileai.portal.controller.wcm;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.portal.bizmoduler.wcm.InfomationManage;
import com.agileai.portal.portlets.PortletCacheManager;

/**
 * generated by miscdp
 */
public class InfomationContentHandler extends SimpleHandler{
	public InfomationContentHandler(){
		super();
	}
	public ViewRenderer prepareDisplay(DataParam param) {
		User user = (User)this.getUser();
		String infomationId = param.get("infomationId");
		String columnId = param.get("columnId");
		this.setAttribute("infomationId", infomationId);
		String tabId = "WcmInfomation";
		DataParam queryParam = new DataParam("INFO_ID",infomationId);
		DataRow record = getService().getContentRecord(tabId,queryParam);
		this.setAttribute("content",record.get("INFO_CONTENT"));
		this.setAttribute("state",record.get("INFO_STATE"));
		this.setAttribute("columnId", columnId);
        String editMode = param.get("editMode");
        String userCode = user.getUserCode();
        this.setAttribute("editMode",editMode); 
        this.setAttribute("userCode",userCode); 
        this.getSessionAttributes().put(InfomationManageListHandler.LastEditTypeSessionKey, InfomationManage.EditType.Content);
		return new LocalRenderer(getPage());
	}
	
	@PageAction
	public ViewRenderer saveInfomationContent(DataParam param){
		String responseText = FAIL;
		InfomationManage infomationManage = getService();
		String content = param.get("content");
		String infomationId = param.get("infomationId");
		infomationManage.saveInfomationContent(infomationId, content);
		
		String urlPrefix = PortletCacheManager.buildDataURLPefix(request);
		String tempateURL = urlPrefix+"/resource?ContentProvider&contentId="+infomationId;
		String shortURL = "/portal/resource?ContentProvider&contentId="+infomationId;;
		PortletCacheManager.getOnly().removeCache(shortURL);
		PortletCacheManager.getOnly().removeCache(infomationId);
		PortletCacheManager.getOnly().removeCache(tempateURL);
		responseText = SUCCESS;
		return new AjaxRenderer(responseText);
	}
    protected InfomationManage getService() {
        return (InfomationManage) this.lookupService(InfomationManage.class);
    }	
}
